package com.egg.mall.controller.web;

import com.egg.common.core.model.controller.WebController;
import com.egg.common.core.model.response.ResponseData;
import com.egg.common.util.JsonUtil;
import com.egg.mall.entity.sku.Sku;
import com.egg.mall.model.request.SkuPut;
import com.egg.mall.model.response.web.SkuInfo;
import com.egg.mall.service.sku.ISkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;


/**
 * <p>
 * 商品SKU 前端控制器
 * </p>
 *
 * @author east
 * @since 2022-08-08
 */
@Slf4j
@Api(tags = WebController.API_PREFIX + "商品SKU")
@RestController
@RequestMapping("/sku")
public class WebSkuController extends WebController {


    @Autowired
    private ISkuService skuService;


    @ApiOperation(value = "SKU信息")
    @GetMapping("/{id}/info")
    public ResponseData<SkuInfo> skuInfoGet(@ApiParam(value = "SKU ID") @PathVariable Long id) {
        Sku dbSku = skuService.baseGetById(id);
        SkuInfo info = JsonUtil.objToObj(dbSku, SkuInfo.class);
        return ResponseData.success(info);
    }

    @ApiOperation(value = "SKU修改")
    @PutMapping("/{id}")
    public ResponseData<Long> skuPut(
            @ApiParam(value = "SKU ID") @PathVariable String id
            , @RequestBody SkuPut edit
    ) {
        Long existId = skuService.edit(id, edit);
        return ResponseData.success(existId);
    }

    @ApiOperation(value = "SKU删除")
    @DeleteMapping
    public ResponseData<String> spuDel(
            @ApiParam(value = "ID集合", required = true) @RequestParam(value = "ids") Collection<String> ids
    ) {
        skuService.baseDel(ids);
        return ResponseData.success();
    }


}

